package foo.bar.wiki.web.action.general;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.nonextension.NullResolution;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.domain.Resource;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import java.io.ByteArrayInputStream;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class ResourceRetrievalActionBean extends BaseActionBean {

    private String resourceName;
    private String spaceId;

    private WikiService wikiService;


    @DefaultHandler
    public Resolution view() {
        Resource resource = wikiService.getResourceByNameAndSpaceId(resourceName, spaceId);
        if (resource == null) {
            return new NullResolution();
        }
        return new StreamingResolution(resource.getMimeType(), new ByteArrayInputStream(resource.getContent()));
    }




    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }


    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public String getSpaceId() {
        return spaceId;
    }

    public void setSpaceId(String spaceId) {
        this.spaceId = spaceId;
    }
}
